<script lang="ts" setup>
import PageDialog from "@/components/PageDialog/index.vue";
import {ref} from "vue";
import {MeterObj} from "@/api/demoClass";
import {addMeter, editMeter, stationList} from "@/api/demoApi";
import {message} from "@/utils/message";

defineOptions({
  name: "meterDialog"
});

/**-------------------表单相关-------------------**/
//表单参数
const formData = ref({
  id: null,
  stationId: null,
  meterNo: null,
  initDegree: null,
  initTime: null,
  brand:null,
  typeCode: null,
  detailAddress: null,
  status: 0
})

//表单校验
const formRules = ref({
  stationId: [{required:true,message:'请选择所属站点',trigger:['blur','change']}],
  meterNo: [{required:true,message:'请输入电表编号',trigger:['blur','change']}],
  initTime: [{required:true,message:'请选择建表日期',trigger:['blur','change']}],
  detailAddress: [{required:true,message:'请输入详细位置',trigger:['blur','change']}]
})

//业主选项
const stationOptions = ref([])
const getStationOptions = ()=>{
  stationOptions.value = stationList().map(item=>({value:item.id,label:`${item.stationNo}(${item.planName})`,disabled:item.status===1}))
}

/**-------------------打开弹框-------------------**/
//标题
const dialogTitle = ref('')
const dialog = ref()
//弹框显示
const dialogShow = (title: string,data?: object)=>{
  dialogTitle.value = title
  if(data) {
    formData.value = {...data}
  }
  getStationOptions()
  dialog.value.dialogVisible = true
}
defineExpose({
  dialogShow
})

/**-------------------事件回调-------------------**/
//取消
const form = ref()
const handlerCancel = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.resetFields()
    formData.value = {
      id: null,
      stationId: null,
      meterNo: null,
      initDegree: null,
      initTime: null,
      brand:null,
      typeCode: null,
      detailAddress: null,
      status: 0
    }
    dialog.value.dialogVisible = false
    resolve({})
  }catch(e){
    console.log(e)
    reject(e)
  }
})
//确认
const emits = defineEmits(['done'])
const handlerConfirm = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.validate()
    //todo 新增、编辑接口
    if(!formData.value.id) {
      const newProject = new MeterObj(formData.value)
      if(formData.value.initDegree) {
        newProject.currentDegree = formData.value.initDegree
      }else {
        newProject.currentDegree = 0.00
      }
      newProject.ownerDone = 0
      newProject.operatorDone = 0
      addMeter(newProject)
      message("添加成功", { type: "success" });
    }else {
      editMeter(formData.value)
      message("编辑成功", { type: "success" });
    }
    await handlerCancel()
    emits('done')
    resolve({})
  }catch(e){
    reject(e)
  }
})
</script>

<template>
  <PageDialog ref="dialog" :handler-cancel="handlerCancel" :handler-confirm="handlerConfirm" :title="dialogTitle">
    <el-form ref="form" :model="formData" :rules="formRules" label-position="top" label-width="auto">
      <el-form-item label="电表号" prop="meterNo">
        <el-input v-model="formData.meterNo" clearable placeholder="请输入电表号" />
      </el-form-item>
      <el-form-item label="所属站点" prop="stationId">
        <el-select v-model="formData.stationId" :disabled="dialogTitle==='编辑电表'" clearable filterable placeholder="请选择所属站点，未完成指派的站点不可选">
          <el-option v-for="(item,index) in stationOptions" :key="index" :disabled="item.disabled" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="初始度数" prop="initDegree">
        <el-input-number v-model.number="formData.initDegree" :controls="false" :disabled="dialogTitle==='编辑电表'" :precision="2" :step="0.01" placeholder="请输入初始度数"/>
      </el-form-item>
      <el-form-item label="建表日期" prop="initTime">
        <el-date-picker
          v-model="formData.initTime"
          :disabled="dialogTitle==='编辑电表'"
          clearable
          format="YYYY-MM-DD"
          placeholder="请选择建表日期"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="formData.brand" clearable placeholder="请输入电表品牌" />
      </el-form-item>
      <el-form-item label="型号" prop="typeCode">
        <el-input v-model="formData.typeCode" clearable placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="详细位置" prop="detailAddress">
        <el-input v-model="formData.detailAddress" clearable placeholder="请输入电表详细位置" />
      </el-form-item>
    </el-form>
  </PageDialog>
</template>

<style lang="scss" scoped>
:deep(.el-form) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;

  .el-form-item {
    width: 48%;

    .el-input,
    .el-select,
    .el-input-number,
    .el-date-picker{
      width: 100%;
    }
  }
}
</style>
